package com.bdqn.t382.interceptors;

import com.bdqn.t382.entity.SysUser;
import com.bdqn.t382.utils.Constants;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;

public class AuthInterceptor implements HandlerInterceptor {
    private static final Logger LOGGER = LoggerFactory.getLogger(AuthInterceptor.class);
 /** 前置处理方法,进入控制器之前经过此方法判断是否能够继续请求 */
         @Override
 public boolean preHandle(HttpServletRequest request,
                          HttpServletResponse response, Object handler) throws Exception {
         LOGGER.debug("进入自定义拦截器 AuthInterceptor，请求路径:{}",
                 request.getRequestURI());
         HttpSession session = request.getSession();
         // 获取登录用户信息
         SysUser sysUser=(SysUser)session.getAttribute(Constants.SESSION_USER);
         // 检查是否登录
         if(null == sysUser){
             // 没有登录信息，跳转到 401.jsp 错误页
             request.getRequestDispatcher("/WEB-INF/jsp/401.jsp")
             .forward(request, response);
             // 返回 false 表示请求被拒绝，在返回之前需要做其他响应操作。
             return false;
             }
             return true;
    }
}
